﻿using MiniFox.Log;
using MiniFox.Net.Models;
using MiniFox.Web;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http.Headers;
using System.Text;

namespace MiniFox.Net
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class Service : IService
    {
        IWebAdapter _webAdapter;
        IServiceSetting _setting;
        Dictionary<string, string> _headers;
        Dictionary<string, string> _parameters;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        /// <param name="webAdapter"></param>
        protected internal Service(IServiceSetting setting, IWebAdapter webAdapter)
        {
            _setting = setting;
            _webAdapter = webAdapter;
            _headers = _setting.Headers;
            _parameters = _setting.UrlParameters;
        }
        /// <summary>
        /// 
        /// </summary>
        protected IServiceSetting Setting
        {
            get { return _setting; }
        }
        /// <summary>
        /// 
        /// </summary>
        public IWebAdapter WebAdapter
        {
            get { return _webAdapter; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get { return _setting.Name; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Url
        {
            get { return _setting.Uri; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Encoding ContentEncoding
        {
            get { return this._setting.ContentEncoding; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected Dictionary<string, string> Headers
        {
            get { return _headers; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected Dictionary<string, string> Parameters
        {
            get { return _parameters; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected TimeSpan Timeout
        {
            get { return TimeSpan.FromSeconds(_setting.Timeout); }
        }
        /// <summary>
        /// 
        /// </summary>
        public abstract Uri FullURI
        {
            get;
        }

        //#region send text

        //#region async
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public Task<string> SendAsync()
        //{

        //    return this.SendAsync(WebRequestMethods.Http.Get);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(string method)
        //{
        //    return this.SendAsync(method, (object)null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(string method, object query)
        //{
        //    return SendAsync(method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<string> SendAsync(string method, object query, object headers)
        //{
        //    WebMessageRequest request = new WebMessageRequest(method, query, headers, this.MediaType);
        //    WebTextResponse response = await this.SendAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebTextResponse> SendAsync(WebMessageRequest request)
        //{
        //    WebTextResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = MappingRequest(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, null));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingTextResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingTextResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(string data, string method)
        //{
        //    return SendAsync(data, method, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(string data, string method, object query)
        //{
        //    return SendAsync(data, method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<string> SendAsync(string data, string method, object query, object headers)
        //{
        //    WebTextRequest request = new WebTextRequest(data, method, query, headers, this.MediaType);
        //    WebTextResponse response = await this.SendAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebTextResponse> SendAsync(WebTextRequest request)
        //{
        //    WebTextResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = await MappingTextRequestAsync(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.Data));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingTextResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingTextResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        //#endregion

        //#endregion

        //#region send byte

        //#region async
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync()
        //{
        //    return SendBytesAsync(WebRequestMethods.Http.Get);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync(string method)
        //{
        //    return SendBytesAsync(method, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync(string method, object query)
        //{
        //    return SendBytesAsync(method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<byte[]> SendBytesAsync(string method, object query, object headers)
        //{
        //    WebMessageRequest request = new WebMessageRequest(method, query, headers, this.MediaType);
        //    WebBinaryResponse response = await this.SendBytesAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public virtual async Task<WebBinaryResponse> SendBytesAsync(WebMessageRequest request)
        //{
        //    WebBinaryResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = MappingRequest(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, null));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingBinaryResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingBinaryResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync(byte[] data)
        //{
        //    return SendBytesAsync(data, WebRequestMethods.Http.Post);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync(byte[] data, string method)
        //{
        //    return SendBytesAsync(data, method, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<byte[]> SendBytesAsync(byte[] data, string method, object query)
        //{
        //    return SendBytesAsync(data, method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<byte[]> SendBytesAsync(byte[] data, string method, object query, object headers)
        //{
        //    WebBinaryRequest request = new WebBinaryRequest(data, method, query, headers, MediaType);
        //    WebBinaryResponse response = await this.SendBytesAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebBinaryResponse> SendBytesAsync(WebBinaryRequest request)
        //{
        //    WebBinaryResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = await MappingBinaryRequestAsync(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.Data));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingBinaryResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingBinaryResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        //#endregion

        //#endregion

        //#region send stream

        //#region async
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync()
        //{
        //    return SendStreamAsync(WebRequestMethods.Http.Get);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync(string method)
        //{
        //    return SendStreamAsync(method, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync(string method, object query)
        //{
        //    return SendStreamAsync(method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<Stream> SendStreamAsync(string method, object query, object headers)
        //{
        //    WebMessageRequest request = new WebMessageRequest(method, query, headers, this.MediaType);
        //    WebStreamResponse response = await this.SendStreamAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public virtual async Task<WebStreamResponse> SendStreamAsync(WebMessageRequest request)
        //{
        //    WebStreamResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = MappingRequest(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, null));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingStreamResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingStreamResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="stream"></param>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync(Stream stream)
        //{
        //    return SendStreamAsync(stream, WebRequestMethods.Http.Post);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="stream"></param>
        ///// <param name="method"></param>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync(Stream stream, string method)
        //{
        //    return SendStreamAsync(stream, method, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="stream"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<Stream> SendStreamAsync(Stream stream, string method, object query)
        //{
        //    return SendStreamAsync(stream, method, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="stream"></param>
        ///// <param name="method"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<Stream> SendStreamAsync(Stream stream, string method, object query, object headers)
        //{
        //    WebStreamRequest request = new WebStreamRequest(stream, method, query, headers);
        //    WebStreamResponse response = await this.SendStreamAsync(request);
        //    return response.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebStreamResponse> SendStreamAsync(WebStreamRequest request)
        //{
        //    WebStreamResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = await MappingStreamRequestAsync(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.Data));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingStreamResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingStreamResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}

        //#endregion

        //#endregion

        //#region send multipart

        //#region sync

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <returns></returns>
        //public string Send(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas)
        //{
        //    return Send(fileDatas, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public string Send(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query)
        //{
        //    return Send(fileDatas, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <param name="headers"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public string Send(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query, object headers)
        //{
        //    var req = new WebMultiPartRequest(fileDatas, query, headers);
        //    var res = Send(req);
        //    return res.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public virtual WebTextResponse Send(WebMultiPartRequest request)
        //{
        //    WebTextResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = MappingMultiPartRequest(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.FileDatas));
        //        WebResponse res = req.GetResponse();
        //        response = MappingTextResponse(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = MappingTextResponse(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <returns></returns>
        //public RS Send<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas)
        //{
        //    return Send<RS>(fileDatas, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public RS Send<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query)
        //{
        //    return Send<RS>(fileDatas, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <param name="headers"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public RS Send<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query, object headers)
        //{
        //    var req = new WebMultiPartRequest(fileDatas, query, headers);
        //    var res = Send<RS>(req);
        //    return res.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public virtual WebObjectResponse<RS> Send<RS>(WebMultiPartRequest request)
        //{
        //    WebObjectResponse<RS> response;
        //    try
        //    {
        //        HttpRequestMessage req = MappingMultiPartRequest(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.FileDatas));
        //        WebResponse res = req.GetResponse();
        //        response = MappingObjectResponse<RS>(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = MappingObjectResponse<RS>(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        //#endregion

        //#region async

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas)
        //{
        //    return SendAsync(fileDatas, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<string> SendAsync(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query)
        //{
        //    return SendAsync(fileDatas, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<string> SendAsync(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query, object headers)
        //{
        //    var req = new WebMultiPartRequest(fileDatas, query, headers);
        //    var res = await SendAsync(req);
        //    return res.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebTextResponse> SendAsync(WebMultiPartRequest request)
        //{
        //    WebTextResponse response;
        //    try
        //    {
        //        HttpRequestMessage req = await MappingMultiPartRequestAsync(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.FileDatas));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingTextResponseAsync(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingTextResponseAsync(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}


        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <returns></returns>
        //public Task<RS> SendAsync<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas)
        //{
        //    return SendAsync<RS>(fileDatas, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <returns></returns>
        //public Task<RS> SendAsync<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query)
        //{
        //    return SendAsync<RS>(fileDatas, query, null);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <typeparam name="RS"></typeparam>
        ///// <param name="fileDatas"></param>
        ///// <param name="query"></param>
        ///// <param name="headers"></param>
        ///// <returns></returns>
        //public async Task<RS> SendAsync<RS>(IEnumerable<(string formName, string fileName, string mimeType, byte[] data)> fileDatas, object query, object headers)
        //{
        //    var req = new WebMultiPartRequest(fileDatas, query, headers);
        //    var res = await SendAsync<RS>(req);
        //    return res.Data;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public async virtual Task<WebObjectResponse<RS>> SendAsync<RS>(WebMultiPartRequest request)
        //{
        //    WebObjectResponse<RS> response;
        //    try
        //    {
        //        HttpRequestMessage req = await MappingMultiPartRequestAsync(request);
        //        this.OnBeforeSend(new BeforeSendEventArgs(req, request.FileDatas));
        //        WebResponse res = await req.GetResponseAsync();
        //        response = await MappingObjectResponseAsync<RS>(res);
        //        this.OnAfterSend(new AfterSendEventArgs(res, response.Data));
        //    }
        //    catch (WebException ex)
        //    {
        //        this.WriteWarnLog(ex.Message, ex);
        //        response = await MappingObjectResponseAsync<RS>(ex.Response);
        //        response.Error = ex;
        //        this.OnAfterSend(new AfterSendEventArgs(ex.Response, response.Data));
        //    }
        //    catch (Exception e)
        //    {
        //        this.WriteFatalLog(e);
        //        throw e;
        //    }
        //    return response;
        //}
        //#endregion

        //#endregion



        #region log

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected void WriteInfoLog(string message, Exception exception)
        {
            this.WebAdapter.Logger.WriteInfo(new DefaultLogContent
            {
                Message = message,
                Exception = exception
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected void WriteWarnLog(string message, Exception exception)
        {
            this.WebAdapter.Logger.WriteWarn(new DefaultLogContent
            {
                Message = message,
                Exception = exception
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="exception"></param>
        protected void WriteErrorLog(Exception exception)
        {
            this.WebAdapter.Logger.WriteError(new DefaultLogContent
            {
                Exception = exception
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="exception"></param>
        protected void WriteFatalLog(Exception exception)
        {
            this.WebAdapter.Logger.WriteFatal(new DefaultLogContent
            {
                Exception = exception
            });
        }
        #endregion
    }

}
